#include "stdafx.h"

bool Graph::render()
{
	for (Line * i : E)
		i->render();

	for (Dot * i : V)
		i->render();

	return true;
}

bool Graph::mouseDown(int button, int x, int y)
{
	if (button == 0)
	{
		for (int i = 0; i < V.size(); i++)
		{
			if (V[i]->collide(x, y))
			{
				flag = i;
				//ox = V[i]->getX();
				//oy = V[i]->getY();
				break;
			}
		}
	}

	if (flag == -1)
	{
		return false;
	}
	else
	{
		return true;
	}
}

bool Graph::mouseUp(int button, int x, int y)
{
	flag = -1;
	return true;
}

bool Graph::mouseMove(int button, int x, int y)
{
	if (button == 1)
	{
		if (flag != -1)
		{
			V[flag]->moveto(x, y);
		}
	}

	return true;
}

void Graph::update()
{
	for (int i = 0; i < E.size(); i++)
	{
		E[i]->setColor(Line::COLOR_T);
	}

	for (int i = 0; i < E.size(); i++)
	{
		for (int j = i + 1; j < E.size(); j++)
		{
			if (E[i]->intersect(E[j]))
			{
				E[i]->setColor(Line::COLOR_F);
				E[j]->setColor(Line::COLOR_F);
			}
		}
	}
}

void Graph::generate(int v, int e)
{
	for (Line * i : E)
		delete i;
	for (Dot * i : V)
		delete i;

	E.clear();
	V.clear();

	srand(time_t(0));
	for (int i = 0; i < v; i++)
	{
		int x, y;

		x = rand() % 1900 + 10;
		y = rand() % 1000 + 40;
		V.push_back(new Dot(x, y));
	}

	bool r[100][100] = { 0 };
	for (int i = 0; i < e; i++)
	{
		int x = 0, y = 0;
		int time = 5;

		while (x == y || r[x][y] == true && time--)
		{
			x = rand() % V.size();
			y = rand() % V.size();
		}

		E.push_back(new Line(V[x], V[y]));
		r[x][y] = true;
	}
}